package com.zu.commu.dataSource.imp;

import com.zu.commu.dataSource.NcsConnection;
import com.zu.commu.dataSource.NcsStatement;
import com.zu.commu.dataSource.imp.pool.NcsDataSourceManager;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.Statement;

public class NcsConnectionImp
  implements NcsConnection
{
  private Connection connection;
  private String connectPoolName;
  private NcsDataSourceManager ncsDataSourceManager;

  public NcsConnectionImp(NcsDataSourceManager ncsDataSourceManager, String connectPoolName, Connection connection)
  {
    this.ncsDataSourceManager = ncsDataSourceManager;
    this.connectPoolName = connectPoolName;
    this.connection = connection;
  }

  public NcsStatement getNcsStatement()
  {
    try
    {
      Statement st = this.connection.createStatement();
      NcsStatementImp ncs = new NcsStatementImp(st);
      return ncs;
    } catch (Exception e) {
      Logger log = Logger.getLogger("com.automic.commu.dataSource.imp");
      log.error("创建数据库会话出错:\n", e);
    }return null;
  }

  public boolean isClosed()
  {
    try
    {
      return this.connection.isClosed(); } catch (Exception e) {
    }
    return true;
  }

  public void commit()
  {
    try
    {
      this.connection.commit();
    } catch (Exception e) {
      Logger log = Logger.getLogger("com.automic.commu.dataSource.imp");
      log.error("提交数据库事务出错!\n", e);
    }
  }

  public void setAutoCommit(boolean flag)
  {
    try
    {
      this.connection.setAutoCommit(flag);
    } catch (Exception e) {
      Logger log = Logger.getLogger("com.automic.commu.dataSource.imp");
      log.error("设置数据库事务提交类型出错!\n", e);
    }
  }

  public void rollback()
  {
    try
    {
      this.connection.rollback();
    } catch (Exception e) {
      Logger log = Logger.getLogger("com.automic.commu.dataSource.imp");
      log.error("回滚数据库事务出错!\n", e);
    }
  }

  public void free()
  {
    this.ncsDataSourceManager.freeConnection(this.connectPoolName, this.connection);
    this.connection = null;
  }

  public void close()
  {
    try
    {
      this.connection.close();
    } catch (Exception e) {
      this.connection = null;
    }
  }
}